﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using IChessEngine;
using IBLL;

namespace ChessControls
{
    /// <summary>
    /// Interaction logic for ChsGameControl.xaml
    /// </summary>
    public partial class ChsGameControl : UserControl
    {
        private Guid gameId;
        private IChsGameEditBLL bll = null;
        private IChsGamePosition currentPosition = null;
        public ChsGameControl()
        {
            InitializeComponent();
            AddHandler(Label.MouseDownEvent, 
                new RoutedEventHandler(OnLabelMouseDown));
        }
        public void SetBll(IChsGameEditBLL _bll, Guid _gameId)
        {
            bll = _bll;
            gameId = _gameId;
            SetCurrentPosition(bll.GetRootPosition(gameId));
        }
        private void SetCurrentPosition(IChsGamePosition position)
        {
            currentPosition = position;
            Board.SetPosition(currentPosition);
        }
        public void Board_Movement(object sender, ChsMovementEventArgs e)
        {
            IChsGameMovement movement =
                bll.AddMovement(gameId, currentPosition, e.Movement);
            if(movement != null)
            {
                SetCurrentPosition(bll.GetTargetPosition(gameId, movement));
                TextBlock label = new TextBlock();
                label.Text = " "+movement.PgnNotation+" ";
                label.Tag = movement.Id;
                Planilla.Children.Add(label);
            }
            else
            {
                //movimiento ilegal
            }
        }
        public  void Root_Click(object sender, RoutedEventArgs e)
        {
            SetCurrentPosition(bll.GetRootPosition(gameId));
        }
        public  void Backward_Click(object sender, RoutedEventArgs e)
        {
            IChsGamePosition backwardPosition =
                bll.FindBackwardPosition(gameId, currentPosition);
            if (backwardPosition != null)
                SetCurrentPosition(backwardPosition);
        }
        public  void Forward_Click(object sender, RoutedEventArgs e)
        {
            List<IChsGameMovement> forwardMovements =
                bll.FindForwardMovements(gameId, currentPosition);
            if(forwardMovements.Count!=0)
                SetCurrentPosition(
                    bll.GetTargetPosition(gameId, forwardMovements[0]));
        }
        private void OnLabelMouseDown(object sender, RoutedEventArgs e)
        {
            if ((e.OriginalSource is TextBlock) && (((TextBlock)(e.OriginalSource)).Tag != null))
            {
                SetCurrentPosition(bll.GetTargetPosition(gameId, (Guid)(((TextBlock)(e.OriginalSource)).Tag)));
            }
        }
    }
}
